import {
  createRouter,
  createWebHistory
} from 'vue-router';

import Login from "@/views/Login.vue";
import Home from "@/views/Home/index.vue";
import News from '@/views/News/index.vue'
import Appeal from '@/views/Appeal/index.vue'
import Order from '@/views/Order/index.vue'
import Service from '@/views/_Service/index.vue'
import admin from '@/views/User/admin.vue'
import wxuser from '@/views/User/wxuser.vue'


const routes = [{
  path: '/login',
  name: 'Login',
  component: Login,
},
{
  path: '/',
  redirect: '/login'
},
{
  path: '/home',
  name: 'Home',
  redirect: '/news',
  component: Home,
  children: [
    {
      path: '/news',
      name: 'News',
      component: News,
      meta: {
        breadcrumb: ['讯息管理']
      }
    },
    {
      path: '/appeal',
      name: 'Appeal',
      component: Appeal,
      meta: {
        breadcrumb: ['居民诉求']
      }
    },
    {
      path: '/resouce',
      name: 'resouce',
      children:[
        {
          path: '/order',
          name: 'Order',
          component: Order,
          meta: {
            breadcrumb: ['社区资源','超市订单']
          }
        },
        {
          path: '/service',
          name: 'Service',
          component: Service,
          meta: {
            breadcrumb: ['社区资源','社区服务']
          }
        },
      ]
    },

    {
      path: '/user',
      name: 'user',
      redirect: '/admin',
      children: [
        {
          path: '/admin',
          name: 'admin',
          component: admin,
          meta: {
            breadcrumb: ['用户管理','管理员']
          }
        },
        {
          path: '/wxuser',
          name: 'wxuser',
          component: wxuser,
          meta: {
            breadcrumb: ['用户管理','居民用户']
          }
        }
      ]
    }

  ]
}


];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next() // 访问登录直接放行
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login') // 检查token是否为空,为空跳转到登录页
  next()
})

export default router;